#ifndef UTILITY_H
#define UTILITY_H

#include <chrono> // high resolution clock
#include <iostream>

using namespace std;
using namespace std::chrono;

// 两种用法
// 反复执行多次同一个算法，取平均值
// 同一个算法，改变输入规模，观察执行时间的变化
class Timer {
public:
  // Constructor
  Timer() {
    start = high_resolution_clock::now();
    end = high_resolution_clock::now();

    _s = high_resolution_clock::now();
    _e = high_resolution_clock::now();
  }

  // Destructor
  ~Timer() {
    end = high_resolution_clock::now();
    // Blue - function name
    // Yellow - generic parameter
    auto duration = duration_cast<microseconds>(end - start);
    cout<<duration.count()<<" microseconds elapsed."<<endl;
  }

  void begin() {
    _s = high_resolution_clock::now();
  }

  long stop() {
    _e = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(_e - _s);

    return duration.count();
  }
private:
  // Attribute
  time_point<high_resolution_clock> start;
  time_point<high_resolution_clock> end;

  time_point<high_resolution_clock> _s;
  time_point<high_resolution_clock> _e;
};

#endif
